package wechat.rcv;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ImageController
{
  static final Logger logger = Logger.getLogger(ImageController.class.getName());
  
  @RequestMapping({"/sample.jpg"})
  @ResponseBody
  public byte[] getFile()
  {
    try
    {
      InputStream is = getClass().getResourceAsStream("/sample.jpg");
      
      BufferedImage img = ImageIO.read(is);
      
      ByteArrayOutputStream bao = new ByteArrayOutputStream();
      
      ImageIO.write(img, "jpg", bao);
      
      return bao.toByteArray();
    }
    catch (IOException e)
    {
      logger.error(e);
      throw new RuntimeException(e);
    }
  }
  
  @RequestMapping({"/smallsample.jpg"})
  @ResponseBody
  public byte[] getSmallImg()
  {
    try
    {
      InputStream is = getClass().getResourceAsStream("/smallsample.jpg");
      
      BufferedImage img = ImageIO.read(is);
      
      ByteArrayOutputStream bao = new ByteArrayOutputStream();
      
      ImageIO.write(img, "jpg", bao);
      
      return bao.toByteArray();
    }
    catch (IOException e)
    {
      logger.error(e);
      throw new RuntimeException(e);
    }
  }
}
